package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import view.admin.AdminDialog;
import view.room.RoomSearcher;

public class ToolBar extends JPanel{
	private static final long serialVersionUID = 1L;
	private final String[] strStuff = {
			"TÌM PHÒNG", "TÌM KHÁCH HÀNG", "THỐNG KÊ"	
	};
	private final JButton[] btStuff = new JButton[strStuff.length];
	
	private JButton btAdmin = new JButton("Administrator");
	
	public ToolBar(){
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(0, 40));
		
		JPanel jpRight = new JPanel();
		FlowLayout loRight = new FlowLayout(FlowLayout.RIGHT);
		jpRight.setLayout(loRight);
		loRight.setAlignOnBaseline(true);
		
		btAdmin.addActionListener(new ButtonHandler());
		jpRight.add(btAdmin);
		
		JPanel jpLeft = new JPanel();
		FlowLayout loLeft = new FlowLayout(FlowLayout.LEFT);
		jpRight.setLayout(loLeft);
		loLeft.setAlignOnBaseline(true);
		
		JLabel lbSpace = new JLabel();
		lbSpace.setPreferredSize(new Dimension(140, 10));
		jpLeft.add(lbSpace);
		
		StuffHandler stuffHandler = new StuffHandler();
		for (int i=0; i<btStuff.length; i++){
			btStuff[i] = new JButton(strStuff[i]);
			btStuff[i].addActionListener(stuffHandler);
			jpLeft.add(btStuff[i]);
		}
		
		add(jpLeft, BorderLayout.WEST);
		add(jpRight, BorderLayout.EAST);
	}
	
	class ButtonHandler implements ActionListener{
		public void actionPerformed(ActionEvent event){
			AdminDialog adminDialog = new AdminDialog((JFrame)SwingUtilities.getRoot(ToolBar.this));
			adminDialog.setVisible(true);
			MainView mainView = (MainView)SwingUtilities.getRoot(ToolBar.this);
			mainView.refreshAll();
		}
	}
	
	class StuffHandler implements ActionListener{
		public void actionPerformed(ActionEvent event){
			int i=0;
			for (i=0; i<btStuff.length; i++)
				if (event.getSource() == btStuff[i])
					break;
			switch (i){
				case 0: new RoomSearcher((JFrame)SwingUtilities.getRoot(ToolBar.this))
								.setVisible(true);break;
                                case 1: new view.find.Customer(new javax.swing.JFrame(), true).setVisible(true);break;
                                
                                case 2: new view.statistic.StatisticsForm(null, true).setVisible(true); break;    
			}
		}
	}
}
